package com.szd.qiongyou20.home.search.modle;

import android.util.Log;

import com.szd.qiongyou20.home.search.bean.AskBean;
import com.szd.qiongyou20.home.search.bean.GoodsBean;
import com.szd.qiongyou20.home.search.bean.JnBean;
import com.szd.qiongyou20.home.search.bean.MddBean;
import com.szd.qiongyou20.home.search.bean.TieZiBean;
import com.szd.qiongyou20.home.search.bean.UserBean;
import com.szd.qiongyou20.home.search.interfaces.ISearch;
import com.szd.qiongyou20.utils.BaseUrl;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by SZD on 2016/12/26.
 */

public class LoadSearchData implements SearchData {

    private ISearch mISearch;
    private Map<String, String> map;
    private String mKeyWord;

    public LoadSearchData(String keyword) {
        mKeyWord = keyword;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        mISearch = retrofit.create(ISearch.class);

        map = new HashMap<>();
        // 相同数据
        map.put("client_id", "qyer_android");
        map.put("client_secret", "9fcaae8aefc4f9ac4915");
        map.put("v", "1");
        map.put("track_user_id", "8769486");
        map.put("track_deviceid", "867271028985116");
        map.put("track_app_version", "7.2.1");
        map.put("track_app_channel", "360m");
        map.put("track_device_info", "OnePlus2");
        map.put("track_os", "Android5.1.1");
        map.put("app_installtime", "1482217233055");
        map.put("lat", "40.063298");
        map.put("lon", "116.346505");
        map.put("count", "20");
        map.put("page", "1");
//        map.put("keyword", mKeyWord); //传递过来的关键字
//        map.put("type",""); 区分目的地 锦囊  帖子

        //map.put("oauth_token","e8904f9be781e1ed7763137bf6d2ea14"); 商品特有
    }

    @Override
    public void searchMddData(final SearchMddListener searchMddListener) {
        Map<String, String> map_Mdd = new HashMap<>();
        map_Mdd.putAll(map);
        // 目的地的type值
        map_Mdd.put("keyword", mKeyWord);
        map_Mdd.put("type", "place");

        Log.i("得到的集合", "searchMddData: ++++" + map_Mdd);
        Call<MddBean> mddData = mISearch.getMddData(map_Mdd);

        mddData.enqueue(new Callback<MddBean>() {
            @Override
            public void onResponse(Call<MddBean> call, Response<MddBean> response) {
                if (response.isSuccess()) {
                    MddBean mddBean = response.body();
                    Log.i("=========", "onResponse: " + mddBean);
                    searchMddListener.searchSuccess(mddBean);
                } else {
                    searchMddListener.searchFaile("获取数据失败!");
                }
            }

            @Override
            public void onFailure(Call<MddBean> call, Throwable t) {
                searchMddListener.searchFaile("网络访问失败!");
            }
        });
    }

    @Override
    public void searchJnData(final SearchJnListener searchJnListener) {
        Map<String, String> map_Jn = new HashMap<>();
        map_Jn.putAll(map);
        map_Jn.put("keyword", mKeyWord);
        map_Jn.put("type", "jn");

        Call<JnBean> jnData = mISearch.getJnData(map_Jn);

        jnData.enqueue(new Callback<JnBean>() {
            @Override
            public void onResponse(Call<JnBean> call, Response<JnBean> response) {
                if (response.isSuccess()) {
                    JnBean body = response.body();
                    searchJnListener.searchSuccess(body);
                }
            }

            @Override
            public void onFailure(Call<JnBean> call, Throwable t) {

            }
        });

    }

    @Override
    public void searchAskData(final SearchAskListener searchAskListener) {
        Map<String, String> map_Ask = new HashMap<>();
        map_Ask.putAll(map);
        map_Ask.put("keyword", mKeyWord);

        Call<AskBean> askData = mISearch.getAskData(map_Ask);

        askData.enqueue(new Callback<AskBean>() {
            @Override
            public void onResponse(Call<AskBean> call, Response<AskBean> response) {
                if (response.isSuccess()) {
                    AskBean askBean = response.body();
                    searchAskListener.searchSuccess(askBean);
                }
            }

            @Override
            public void onFailure(Call<AskBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void searchTieziData(final SearchTieziListener searchTieziListener) {
        Map<String, String> map_Tiezi = new HashMap<>();
        map_Tiezi.putAll(map);
        map_Tiezi.put("keyword", mKeyWord);
        map_Tiezi.put("type", "thread");

        Call<TieZiBean> tieziData = mISearch.getTieziData(map_Tiezi);

        tieziData.enqueue(new Callback<TieZiBean>() {
            @Override
            public void onResponse(Call<TieZiBean> call, Response<TieZiBean> response) {
                if (response.isSuccess()) {
                    TieZiBean body = response.body();
                    searchTieziListener.searchSuccess(body);
                }
            }

            @Override
            public void onFailure(Call<TieZiBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void searchGoodsData(final SearchGoodsListener searchGoodsListener) {
        Map<String, String> map_Goods = new HashMap<>();
        map_Goods.putAll(map);
        map_Goods.put("keyword", mKeyWord);
        map_Goods.put("oauth_token", "e8904f9be781e1ed7763137bf6d2ea14");

        Call<GoodsBean> goodsData = mISearch.getGoodsData(map_Goods);
        goodsData.enqueue(new Callback<GoodsBean>() {
            @Override
            public void onResponse(Call<GoodsBean> call, Response<GoodsBean> response) {
                if (response.isSuccess()) {
                    GoodsBean body = response.body();
                    searchGoodsListener.searchSuccess(body);
                }
            }

            @Override
            public void onFailure(Call<GoodsBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void searchUserData(final SearchUserListener searchUserListener) {
        Map<String, String> map_User = new HashMap<>();
        map_User.putAll(map);
        map_User.put("keyword", mKeyWord);
        map_User.put("type", "user");

        Call<UserBean> userData = mISearch.getUserData(map_User);

        userData.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if (response.isSuccess()) {
                    UserBean body = response.body();
                    searchUserListener.searchSuccess(body);
                }
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {

            }
        });
    }
}
